﻿Imports System.ComponentModel

Public Class VM_Group
	Inherits AutomaticNotfiyPropertyChanged
	Implements IDisposable

	Dim db As New CongregationDataContainerLocal
	Dim workingGroup As Group

	Public Property GroupName As String
		Get
			Return workingGroup.GroupName
		End Get
		Set(value As String)
			workingGroup.GroupName = value
			db.SaveChanges()
		End Set
	End Property

	Public ReadOnly Property GroupID As Long
		Get
			Return workingGroup.GroupID
		End Get
	End Property

	Public Property OverseerID As Long
		Get
			Return workingGroup.GroupOverseerID
		End Get
		Set(value As Long)
			workingGroup.GroupOverseerID = value
			db.SaveChanges()
		End Set
	End Property

	Public ReadOnly Property OverseerName As String
		Get
			Return workingGroup.PublisherData.FullName
		End Get
	End Property

	Public Property AssistantID As Long
		Get
			Return workingGroup.GroupAssistantID
		End Get
		Set(value As Long)
			workingGroup.GroupAssistantID = value
			db.SaveChanges()
		End Set
	End Property

	Public ReadOnly Property AssistantName As String
		Get
			Return workingGroup.PublisherData1.FullName
		End Get
	End Property

	Public ReadOnly Property GroupMembers As VM_PublisherInformation()
		Get
			Dim returnList As New List(Of VM_PublisherInformation)
			Dim removeList As New List(Of PublisherData)
			For Each publisher In workingGroup.PublisherDatas
				If publisher.IsMoveOut Then
					removeList.Add(publisher)
				Else
					returnList.Add(New VM_PublisherInformation(publisher.PublisherID))
				End If
			Next

			For Each publisher In removeList
				RemoveMember(publisher.PublisherID)
			Next

			Return returnList.OrderBy(Function(x) x.FullName).ToArray
		End Get
	End Property

	Public ReadOnly Property GroupStats As VM_PublisherCardRecord()
		Get
			Dim returnList As New List(Of VM_PublisherCardRecord)
			For Each publisher In workingGroup.PublisherDatas
				For Each record In publisher.PublisherCardDatas
					returnList.Add(New VM_PublisherCardRecord(publisher.PublisherID, record.Month))
				Next
			Next

			Return returnList.ToArray
		End Get
	End Property

	Public Shared ReadOnly Property AllGroups As VM_Group()
		Get
			Dim returnList As New List(Of VM_Group)
			Using db As New CongregationDataContainerLocal
				For Each myGroup In db.Groups
					returnList.Add(New VM_Group(myGroup.GroupID))
				Next
			End Using

			Return returnList.OrderBy(Function(x) x.GroupName).ToArray
		End Get
	End Property

	Public Shared ReadOnly Property UnGroupedPublishers As VM_PublisherInformation()
		Get
			Dim returnList As New List(Of VM_PublisherInformation)
			For Each publisher In PublisherData.LocalPublishers.Where(Function(x) x.GroupID Is Nothing)
				returnList.Add(New VM_PublisherInformation(publisher.PublisherID))
			Next

			Return returnList.OrderBy(Function(x) x.FullName).ToArray
		End Get
	End Property

	Public Shared ReadOnly Property PotentialOverseers As VM_PublisherInformation()
		Get
			Dim returnList As New List(Of VM_PublisherInformation)
			For Each publisher In PublisherData.LocalPublishers.Where(Function(x) x.IsElder OrElse x.IsMinisterialServant)
				returnList.Add(New VM_PublisherInformation(publisher.PublisherID))
			Next

			Return returnList.OrderBy(Function(x) x.FullName).ToArray
		End Get
	End Property

	Public Sub New(GroupID As Long)
		workingGroup = db.Groups.SingleOrDefault(Function(x) x.GroupID = GroupID)

	End Sub

	Public Sub New()
		workingGroup = New Group
		workingGroup.GroupName = String.Format("New Group - {0}", Now)
		db.Groups.AddObject(workingGroup)
		db.SaveChanges()
	End Sub

	Public Sub Save()
		'check to make sure we have all required information before saving
		If workingGroup.GroupName IsNot Nothing AndAlso workingGroup.GroupOverseerID > 0 AndAlso workingGroup.GroupAssistantID > 0 Then
			db.SaveChanges()
		End If
	End Sub

	Public Sub Delete()
		db.Groups.DeleteObject(workingGroup)
		db.SaveChanges()
	End Sub

	Public Sub AddMember(PublisherID As Long)
		Dim workingPublisher = db.PublisherDatas.SingleOrDefault(Function(x) x.PublisherID = PublisherID)

		If workingPublisher IsNot Nothing Then
			workingPublisher.GroupID = GroupID
			db.SaveChanges()
		End If
	End Sub

	Public Sub RemoveMember(PublisherID As Long)
		Dim workingPublisher = db.PublisherDatas.SingleOrDefault(Function(x) x.PublisherID = PublisherID)

		If workingPublisher IsNot Nothing Then
			workingPublisher.GroupID = Nothing
			db.SaveChanges()
		End If
	End Sub

#Region "IDisposable Support"
	Private disposedValue As Boolean ' To detect redundant calls

	' IDisposable
	Protected Overridable Sub Dispose(disposing As Boolean)
		If Not Me.disposedValue Then
			If disposing Then
				' TODO: dispose managed state (managed objects).
				db.Dispose()
			End If

			' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
			' TODO: set large fields to null.
		End If
		Me.disposedValue = True
	End Sub

	' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
	'Protected Overrides Sub Finalize()
	'    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
	'    Dispose(False)
	'    MyBase.Finalize()
	'End Sub

	' This code added by Visual Basic to correctly implement the disposable pattern.
	Public Sub Dispose() Implements IDisposable.Dispose
		' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
		Dispose(True)
		GC.SuppressFinalize(Me)
	End Sub
#End Region

End Class
